from django.db import models


class OrderInfo(models.Model):
    '''邮米订单'''

    STATE_CHOICES = (
        (1, '待付款'),
        (2, '待核销'),
        (3, '已核销'),
        (4, '订单过期'),
        (5, '售后中'),
        (6, '待收货'),
        (7, '已预约'),
        (8, '已退款'),
        (9, '待发货'),
        (10, '退款中'),
        (11, '订单异常'),
        (12, '已付款'),
        (13, '已取消'),
        (14, '已完成'),
        (15, '待成团')
    )

    STATE_ENUM = {
        'WAIT': 1,
        'PAID': 2,
        'SUCCESS': 3,
        'EXPIRED': 4,
        'AFTERSALE': 5,
        'SHIP': 6,
        'RESERVE': 7,
        'REFUNDED': 8
    }

    PLATFORM_CHOICES = (
        (1, '广东'),
        (2, '广州')
    )

    ORDER_TYPE_CHOICES = (
        (1, '线下核销'),
        (2, '消费金'),
        (3, '兑换券'),
        (4, '线下核销(不推送)'),
        (5, '快递配送'),
        (7, '拼团'),
        (8, '盛大订单')
    )

    PAY_TYPE_CHOICES = (
        (1, '支付宝'),
        (2, '微信')
    )

    ORDER_SPECIES_CHOICES = (
        (1, '邮乐享'),
        (2, '微邮惠商圈'),
        (3, '小邮美宿')
    )

    SETTLEMENT_STATE_CHOICES = (
        (1, '未推送'),
        (2, '已推送'),
        (3, '推送失败'),
        (4, '风控')
    )

    IS_PROFIT_SHARING_CHOICES = (
        (0, '不用分账'),
        (1, '分账'),
        (2, '已分账'),
        (3, '冻结订单'),
        (4, '已解冻')
    )

    order_id = models.CharField(verbose_name='平台id', max_length=32, unique=True)
    orderNum = models.CharField(verbose_name='订单号', max_length=64, null=True, blank=True)
    trade_no = models.CharField(verbose_name='订单号', max_length=32, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='下单时间')
    state = models.SmallIntegerField(verbose_name='订单状态', choices=STATE_CHOICES)
    success_time = models.DateTimeField(verbose_name='完成时间', null=True, blank=True)
    payment_time = models.DateTimeField(verbose_name='付款时间', null=True, blank=True)
    cancel_time = models.DateTimeField(verbose_name='付款时间', null=True, blank=True)
    customer_user = models.ForeignKey('users.CustomerUser', verbose_name='下单用户', on_delete=models.SET_NULL, null=True, blank=True)
    customeruservice = models.ForeignKey('users.CustomerUserVice', verbose_name='下单用户', on_delete=models.SET_NULL, null=True, blank=True)
    total_point = models.DecimalField(verbose_name='邮米', max_digits=10, decimal_places=0, default=0)
    total_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='订单价格', default=0)
    realpay_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='实付金额', default=0)
    level = models.ForeignKey('users.Level', on_delete=models.SET_NULL, verbose_name='层级', null=True, blank=True)
    platform = models.SmallIntegerField(verbose_name='平台', choices=PLATFORM_CHOICES, default=1, blank=True)
    companyuser = models.ForeignKey('users.User', verbose_name='核销客户经理', on_delete=models.SET_NULL, null=True, blank=True)
    merchantuser = models.ForeignKey('merchants.MerchantUser', verbose_name='核销商户', on_delete=models.SET_NULL, null=True, blank=True)
    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户', on_delete=models.SET_NULL, null=True, blank=True)
    total_count = models.IntegerField(default=1, verbose_name="商品总数")
    payable_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='应付付金额', default=0)
    address = models.ForeignKey('users.CustomerUserAddress', verbose_name="收货地址", on_delete=models.SET_NULL, null=True, blank=True)
    freight = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="运费", default=0)
    order_type = models.SmallIntegerField(verbose_name='商品下单类型', choices=ORDER_TYPE_CHOICES, default=1, blank=True)
    reserved_mobile = models.CharField(verbose_name='预留手机号', max_length=11, null=True, blank=True)
    bill = models.ForeignKey('merchants.MerchantBill', verbose_name='账单', on_delete=models.SET_NULL, null=True, blank=True)
    monthly_bill = models.ForeignKey('merchants.MerchantMonthlyBill', verbose_name='月度账单', on_delete=models.SET_NULL, null=True, blank=True)
    pay_type = models.SmallIntegerField(verbose_name='付款类型', choices=PAY_TYPE_CHOICES, null=True, blank=True)
    payable_settled_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='应结算金额', default=0)
    bill_order_id = models.CharField(verbose_name='结算订单号', max_length=32, null=True, blank=True)
    bill_error_msg = models.CharField(verbose_name='结算失败原因', max_length=256, null=True, blank=True)
    use_time = models.DateTimeField(verbose_name='核销时间', null=True, blank=True)
    order_species = models.SmallIntegerField(verbose_name='商品下单类型', choices=ORDER_SPECIES_CHOICES, default=1)
    settlement_state = models.SmallIntegerField(verbose_name='订单状态', choices=SETTLEMENT_STATE_CHOICES, null=True, blank=True)
    refund_id = models.CharField(verbose_name='微信支付退款单号', max_length=32, null=True, blank=True)
    out_refund_no = models.CharField(verbose_name='商户退款单号', max_length=32, null=True, blank=True)
    is_profit_sharing = models.SmallIntegerField(verbose_name='是否分账', choices=IS_PROFIT_SHARING_CHOICES, null=True, blank=True)
    refund_money = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="退款金额", default=0)
    group = models.ForeignKey('orders.OrderInfoGROUP', verbose_name='拼团', on_delete=models.SET_NULL, null=True, blank=True)
    ownership = models.CharField(verbose_name='小邮美宿标识', max_length=32, null=True, blank=True)

    class Meta:
        db_table = 'tb_order_info'
        verbose_name = '邮米订单'
        verbose_name_plural = verbose_name


class OrderInfoGROUP(models.Model):
    '''拼团订单'''

    GROUP_STATUS_CHOICES = (
        (0, '未开始'),
        (1, '进行中'),
        (2, '拼团成功'),
        (3, '拼团失败')
    )

    create_time = models.DateTimeField(verbose_name='发起拼团时间', auto_now_add=True)
    group_status = models.SmallIntegerField(verbose_name='拼团状态', choices=GROUP_STATUS_CHOICES, default=0)
    goods = models.ForeignKey('goods.Goods', verbose_name='拼团商品', on_delete=models.CASCADE)
    account = models.CharField(verbose_name='团标识', max_length=32, unique=True)
    expired_time = models.DateTimeField(verbose_name='失效时间')
    customeruservice = models.ForeignKey('users.CustomerUserVice', verbose_name='下单用户', on_delete=models.SET_NULL, null=True, blank=True)
    order_id = models.CharField(verbose_name='主订单', max_length=8, null=True, blank=True)
    qrcode = models.ImageField(verbose_name='拼团二维码', null=True, blank=True)
    share_pic = models.ImageField(verbose_name='分享二维码', null=True, blank=True)
    group_order_num = models.IntegerField(verbose_name='成团数', default=1)

    class Meta:
        db_table = 'tb_order_info_group'
        verbose_name = '邮米订单'
        verbose_name_plural = verbose_name


class AfterSaleReason(models.Model):
    '''申请售后原因'''

    REASON_TYPE_CHOICES = (
        (1, '退款'),
        (2, '退货'),
        (3, '换货')
    )

    IS_DELETE_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    reason_type = models.SmallIntegerField(verbose_name='售后原因', choices=REASON_TYPE_CHOICES, default=1)
    name = models.CharField(verbose_name='原因描述', max_length=32)
    is_delete = models.SmallIntegerField(verbose_name='是否删除', choices=IS_DELETE_CHOICES, default=0)

    class Meta:
        db_table = 'tb_o_after_sale_reason'
        verbose_name = '申请售后原因'
        verbose_name_plural = verbose_name


class AfterSaleApplicationForm(models.Model):
    '''售后申请单'''

    FORM_STATUS_CHOICES = (
        (1, '待退款'),
        (2, '退款成功'),
        (3, '退款失败'),
        (4, '已取消'),
        (5, '待退货'),
        (6, '换货成功'),
        (7, '待发货'),
        (8, '申请中'),
        (9, '商家待收货'),
        (10, '用户待收货'),
        (11, '驳回申请')
    )

    ORDER_STATUS_CHOICES = (
        (1, '待付款'),
        (2, '待核销'),
        (3, '已核销'),
        (4, '订单过期'),
        (5, '售后中'),
        (6, '待收货'),
        (7, '已预约'),
        (8, '已退款'),
        (9, '待发货'),
        (10, '退款中'),
        (11, '订单异常'),
        (12, '已付款'),
        (13, '已取消'),
        (14, '已完成')
    )

    REASON_TYPE_CHOICES = (
        (1, '仅退款'),
        (2, '退货退款'),
        (3, '换货')
    )

    order = models.ForeignKey('orders.OrderInfo', verbose_name='所属订单', on_delete=models.CASCADE)
    order_status = models.SmallIntegerField(verbose_name='原订单状态', choices=ORDER_STATUS_CHOICES)
    form_status = models.SmallIntegerField(verbose_name='申请单状态', choices=FORM_STATUS_CHOICES, default=1)
    create_time = models.DateTimeField(verbose_name='发起售后时间', auto_now_add=True)
    description = models.TextField(verbose_name='原因描述', null=True, blank=True)
    refund_money = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="退款金额", default=0)
    return_msg = models.CharField(verbose_name='失败原因', max_length=256, null=True, blank=True)
    refund_time = models.DateTimeField(verbose_name='退款时间', null=True, blank=True)
    reason = models.ForeignKey('orders.AfterSaleReason', verbose_name='售后原因', on_delete=models.CASCADE)
    application_num = models.CharField(verbose_name='编码', max_length=32)
    customeruservice = models.ForeignKey('users.CustomerUserVice', verbose_name='申请用户', on_delete=models.SET_NULL, null=True, blank=True)
    reason_type = models.SmallIntegerField(verbose_name='售后原因', choices=REASON_TYPE_CHOICES, default=1)
    expired_time = models.DateTimeField(verbose_name='最后寄货时间', null=True, blank=True)

    class Meta:
        db_table = 'tb_o_after_sale_application_form'
        verbose_name = '售后申请单'
        verbose_name_plural = verbose_name


class AfterSaleApplicationFormImg(models.Model):
    '''售后申请单证明资料'''

    applicationform = models.ForeignKey('orders.AfterSaleApplicationForm', verbose_name='售后申请单', on_delete=models.CASCADE)
    img = models.ImageField(verbose_name='图片')

    class Meta:
        db_table = 'tb_o_after_sale_application_form_img'
        verbose_name = '售后申请单证明资料'
        verbose_name_plural = verbose_name


class RefundOrderInfo(models.Model):
    '''退款订单'''

    REFUND_STATUS_CHOICES = (
        (1, '成功'),
        (2, '失败')
    )

    OPERATOR_ROLE_CHOICES = (
        (1, '超管'),
        (2, '机构管理员'),
        (3, '客户经理'),
        (4, '商户'),
        (5, '机器人'),
        (6, '消费者')
    )

    REFUND_TYPE_CHOICES = (
        (1, '全额退款'),
        (2, '部分退款')
    )

    refund_status = models.SmallIntegerField(verbose_name='退款状态', choices=REFUND_STATUS_CHOICES, default=1)
    order = models.ForeignKey('orders.OrderInfo', verbose_name='所属订单', on_delete=models.CASCADE)
    user = models.ForeignKey('users.User', verbose_name='操作人', on_delete=models.SET_NULL, null=True, blank=True)
    refund_order_id = models.CharField(verbose_name='退款订单', max_length=32)
    refund_transaction_id = models.CharField(verbose_name='退款回调订单', max_length=32, null=True, blank=True)
    plat_refund_order_id = models.CharField(verbose_name='退款平台订单', max_length=32, null=True, blank=True)
    refund_money = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="退款金额", default=0)
    refund_procedure_money = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="退款手续费", default=0)
    refund_time = models.DateTimeField(verbose_name='退款时间', null=True, blank=True)
    return_msg = models.CharField(verbose_name='失败原因', max_length=256, null=True, blank=True)
    remark = models.TextField(verbose_name='退款备注', null=True, blank=True)
    merchantuser = models.ForeignKey('merchants.MerchantUser', verbose_name='操作人', on_delete=models.SET_NULL, null=True, blank=True)
    operator_role = models.SmallIntegerField(verbose_name='操作人角色', choices=OPERATOR_ROLE_CHOICES, default=1)
    refund_type = models.SmallIntegerField(verbose_name='退款类型', choices=REFUND_TYPE_CHOICES, default=1)
    customeruservice = models.ForeignKey('users.CustomerUserVice', verbose_name='操作人', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_o_refund_order_info'
        verbose_name = '退款订单'
        verbose_name_plural = verbose_name


class OrderAddress(models.Model):
    '''订单配送地址'''

    order = models.OneToOneField('orders.OrderInfo', verbose_name='所属订单', on_delete=models.CASCADE)
    receiver = models.CharField(max_length=20, verbose_name='收货人')
    province  = models.CharField(max_length=20, verbose_name='省')
    city = models.CharField(max_length=20, verbose_name='市')
    district = models.CharField(max_length=20, verbose_name='区')
    place = models.CharField(max_length=50, verbose_name='地址')
    mobile = models.CharField(max_length=11, verbose_name='手机')
    longitude = models.CharField(verbose_name='经度', max_length=32, null=True, blank=True)
    latitude = models.CharField(verbose_name='纬度', max_length=32, null=True, blank=True)

    class Meta:
        db_table = 'tb_order_address'
        verbose_name = '邮米订单'
        verbose_name_plural = verbose_name


class OrderProfitSharing(models.Model):
    '''分账'''

    PROFIT_STATUS_CHOICES = (
        (1, '分账成功'),
        (2, '分账失败')
    )

    order = models.OneToOneField(OrderInfo, on_delete=models.CASCADE, verbose_name="订单")
    out_trade_no = models.CharField(verbose_name='分账订单', max_length=32, null=True, blank=True)
    profit_order_id = models.CharField(verbose_name='分账回传订单', max_length=32, null=True, blank=True)
    profit_time = models.DateTimeField(verbose_name='分账时间', null=True, blank=True)
    error_msg = models.CharField(verbose_name='分账失败说明', max_length=128, null=True, blank=True)
    profit_status = models.SmallIntegerField(verbose_name='分账状态', choices=PROFIT_STATUS_CHOICES, default=1)

    class Meta:
        db_table = 'tb_order_profit_sharing'
        verbose_name = '订单分账'
        verbose_name_plural = verbose_name


class OrderGoods(models.Model):
    """订单商品"""

    SCORE_CHOICES = (
        (0, '0分'),
        (1, '20分'),
        (2, '40分'),
        (3, '60分'),
        (4, '80分'),
        (5, '100分'),
    )

    IS_ANONYMOUS_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    IS_COMMENTED_CHOICES = (
        (0, '否'),
        (1, '是')
    )

    order = models.ForeignKey(OrderInfo, on_delete=models.CASCADE, verbose_name="订单")
    launched_goods = models.ForeignKey('goods.LaunchedGoods', on_delete=models.PROTECT, verbose_name="订单商品", null=True, blank=True)
    goods = models.ForeignKey('goods.Goods', on_delete=models.SET_NULL, verbose_name="订单商品", null=True, blank=True)
    count = models.IntegerField(default=1, verbose_name="数量")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="单价", default=0, blank=True)
    comment = models.TextField(default="", verbose_name="评价信息")
    score = models.SmallIntegerField(choices=SCORE_CHOICES, default=5, verbose_name='满意度评分')
    is_anonymous = models.SmallIntegerField(default=0, verbose_name='是否匿名评价', choices=IS_ANONYMOUS_CHOICES, blank=True)
    is_commented = models.SmallIntegerField(default=0, verbose_name='是否评价了', choices=IS_COMMENTED_CHOICES, blank=True)
    point = models.DecimalField(verbose_name='邮米', max_digits=10, decimal_places=0, default=0)
    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户', on_delete=models.SET_NULL, null=True, blank=True)
    comment_time = models.DateTimeField(verbose_name='评价时间', null=True, blank=True)

    class Meta:
        db_table = "tb_order_goods"
        verbose_name = '订单商品'
        verbose_name_plural = verbose_name


class OrderGoodsCommentFile(models.Model):
    '''评价文件'''

    FILE_TYPE_CHOICES = (
        (1, '图片'),
        (2, '视频')
    )

    ordergoods = models.ForeignKey('orders.OrderGoods', verbose_name='订单商品', on_delete=models.CASCADE)
    file = models.ImageField(verbose_name='文件')
    file_type = models.SmallIntegerField(verbose_name='文件类型', choices=FILE_TYPE_CHOICES, default=1)

    class Meta:
        db_table = "tb_order_goods_comment_file"
        verbose_name = '评价文件'
        verbose_name_plural = verbose_name


class BankType(models.Model):
    '''银行卡对应type'''

    bank_code = models.CharField(verbose_name='字符型银行编码', max_length=32)
    bank_name = models.CharField(verbose_name='银行', max_length=32)

    class Meta:
        db_table = 'tb_bank_type'
        verbose_name = '银行卡对应type'
        verbose_name_plural = verbose_name


class FundBill(models.Model):
    '''支付宝支付金额信息'''

    FUND_CHANNEL_CHOICES = (
        (1, '支付宝红包'),
        (2, '支付宝余额'),
        (3, '集分宝'),
        (4, '折扣券'),
        (5, '预付卡'),
        (6, '余额宝'),
        (7, '商户储值卡'),
        (8, '商户优惠券'),
        (9, '商户红包'),
        (10, '蚂蚁花呗'),
        (11, '微信零钱'),
        (12, '微信信用卡'),
        (13, '微信借记卡'),
        (14, '微信其他')
    )

    FUND_CHANNEL_ENUM = {
        'COUPON': 1,
        'ALIPAYACCOUNT': 2,
        'POINT': 3,
        'DISCOUNT': 4,
        'PCARD': 5,
        'FINANCEACCOUNT': 6,
        'MCARD': 7,
        'MDISCOUNT': 8,
        'MCOUPON': 9,
        'PCREDIT': 10
    }

    ali_order = models.ForeignKey(OrderInfo, verbose_name='对应订单', on_delete=models.CASCADE)
    amount = models.DecimalField(verbose_name='支付金额', max_digits=10, decimal_places=2)
    fundChannel = models.SmallIntegerField(verbose_name='订单当前状态', choices=FUND_CHANNEL_CHOICES, default=1)
    bank_code = models.CharField(verbose_name='付款方式', null=True, blank=True, max_length=20)

    class Meta:
        db_table = 'tb_order_fund_bill'
        verbose_name = '支付金额信息'
        verbose_name_plural = verbose_name


class VoucherDetail(models.Model):
    '''代金券'''

    ali_order = models.ForeignKey(OrderInfo, verbose_name='对应订单', on_delete=models.CASCADE)
    amount = models.DecimalField(verbose_name='面额', max_digits=10, decimal_places=2)
    merchantContribute = models.DecimalField(verbose_name='商户出资', max_digits=10, decimal_places=2)
    name = models.CharField(verbose_name='券名称', max_length=32)
    otherContribute = models.DecimalField(verbose_name='其他出资', max_digits=10, decimal_places=2)
    voucherId = models.CharField(verbose_name='券ID', max_length=32)
    v_type = models.CharField(verbose_name='券类型', max_length=32)

    class Meta:
        db_table = 'tb_order_voucher_detail'
        verbose_name = '代金券'
        verbose_name_plural = verbose_name


class OrderFundTrans(models.Model):
    '''支付宝付款'''

    PRODUCT_CODE_CHOICES = (
        (1, '单笔无密转账到支付宝账户'),
        (2, '单笔无密转账到银行卡'),
        (3, '收发现金红包')
    )

    PRODUCT_CODE_ENUM = {
        1: 'TRANS_ACCOUNT_NO_PWD',
        2: 'TRANS_BANKCARD_NO_PWD',
        3: 'STD_RED_PACKET'
    }

    BIZ_SCENE_CHOICES = (
        (1, '单笔无密转账到支付宝，B2C现金红包'),
        (2, 'C2C现金红包-领红包')
    )

    BIZ_SCENE_ENUM = {
        1: 'DIRECT_TRANSFER',
        2: 'PERSONAL_COLLECTION'
    }

    IDENTITY_TYPE_CHOICES = (
        (1, '支付宝的会员ID'),
        (2, '支付宝登录号，支持邮箱和手机号格式')
    )

    IDENTITY_TYPE__ENUM = {
        1: 'ALIPAY_USER_ID',
        2: 'ALIPAY_LOGON_ID'
    }

    STATUS_CHOICES = (
        (1, '待转账'),
        (2, '转账成功'),
        (3, '处理中'),
        (4, '失败'),
        (5, '退票')
    )

    STATUS_ENUM = {
        'SUCCESS': 2,
        'FAIL': 4,
        'DEALING': 3,
        'REFUND': 5
    }

    out_biz_no = models.CharField(verbose_name='商家订单号', max_length=64)
    exchange_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='兑换金额')
    product_code = models.SmallIntegerField(verbose_name='业务产品码', choices=PRODUCT_CODE_CHOICES, default=1, blank=True)
    biz_scene = models.SmallIntegerField(verbose_name='特定的业务场景', choices=BIZ_SCENE_CHOICES, default=1, blank=True)
    order_title = models.CharField(verbose_name='转账标题', max_length=128)
    identity = models.CharField(verbose_name='参与方的唯一标识', max_length=64)
    identity_type = models.SmallIntegerField(verbose_name='参与方的标识类型', choices=IDENTITY_TYPE_CHOICES, default=1, blank=True)
    name = models.CharField(verbose_name='参与方的唯一标识', max_length=128, null=True, blank=True)
    remark = models.CharField(verbose_name='业务备注', max_length=200, null=True, blank=True)
    pay_fund_order_id = models.CharField(verbose_name='支付宝支付资金流水号', max_length=32, null=True, blank=True)
    ali_order_id = models.CharField(verbose_name='支付宝转账订单号', max_length=32, null=True, blank=True)
    trans_date = models.DateTimeField(verbose_name='订单支付时间', null=True, blank=True)
    sub_msg = models.TextField(verbose_name='失败说明', null=True, blank=True)
    goods = models.ForeignKey('goods.Goods', on_delete=models.CASCADE, verbose_name='商品')
    order = models.ForeignKey(OrderInfo, on_delete=models.CASCADE, verbose_name="订单")
    status = models.SmallIntegerField(verbose_name='特定的业务场景', choices=STATUS_CHOICES, default=1, blank=True)

    class Meta:
        db_table = 'tb_order_fund_trans'
        verbose_name = '支付宝付款'
        verbose_name_plural = verbose_name


class SYOrderInfo(models.Model):
    '''上邑订单'''

    ORDER_STATUS_CHOICES = (
        (1, '待同步'),
        (2, '已取消'),
        (3, '已同步'),
        (4, '同步失败')
    )

    shop_id = models.CharField(verbose_name='微邮付md5商户号', max_length=32)
    shop_order_id = models.CharField(verbose_name='订单号', max_length=32)
    money = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='补贴金额', default=0)
    context = models.CharField(verbose_name='备注', max_length=256, null=True, blank=True)
    channel = models.CharField(verbose_name='通道', max_length=32, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='下单时间', auto_now_add=True)
    order_status = models.SmallIntegerField(verbose_name='业务产品码', choices=ORDER_STATUS_CHOICES, default=1, blank=True)
    source = models.CharField(verbose_name='订单号', max_length=8)
    repeal_time = models.DateTimeField(verbose_name='下单时间', null=True, blank=True)
    order = models.OneToOneField('orders.OrderInfo', verbose_name='同步订单id', on_delete=models.SET_NULL, null=True, blank=True)
    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户id', on_delete=models.SET_NULL, null=True, blank=True)
    level = models.ForeignKey('users.Level', verbose_name='层级id', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_order_sy'
        verbose_name = '上邑订单'
        verbose_name_plural = verbose_name


class ResidentialHostelOrderInfo(models.Model):
    '''小美邮宿订单'''

    ORDER_STATUS_CHOICES = (
        (1, '待同步'),
        (2, '已取消'),
        (3, '已同步'),
        (4, '同步失败')
    )

    order_status = models.SmallIntegerField(verbose_name='业务产品码', choices=ORDER_STATUS_CHOICES, default=1, blank=True)
    shop_id = models.CharField(verbose_name='微邮付md5商户号', max_length=32)
    business_no = models.CharField(verbose_name='微邮付商户号', max_length=32)
    merchant_name = models.CharField(verbose_name='微邮付商户全称', max_length=64, null=True, blank=True)
    shop_order_id = models.CharField(verbose_name='订单号', max_length=32)
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='补贴金额', default=0)
    create_time = models.DateTimeField(verbose_name='下单时间')
    add_time = models.DateTimeField(verbose_name='上传时间', auto_now_add=True)
    success_time = models.DateTimeField(verbose_name='核销时间', null=True, blank=True)
    goods_name = models.CharField(verbose_name='订单号', max_length=64)
    goods_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品单价', default=0)
    goods_count = models.IntegerField(default=1, verbose_name="数量")
    buyer_name = models.CharField(verbose_name='客户的姓名', max_length=256, null=True, blank=True)
    buyer_mobile = models.CharField(verbose_name='手机号', max_length=11, null=True, blank=True)
    arrive_time = models.DateTimeField(verbose_name='核销时间', null=True, blank=True)
    leave_time = models.DateTimeField(verbose_name='核销时间', null=True, blank=True)
    context = models.CharField(verbose_name='备注', max_length=256, null=True, blank=True)
    order = models.OneToOneField('orders.OrderInfo', verbose_name='同步订单id', on_delete=models.SET_NULL, null=True, blank=True)
    merchant = models.ForeignKey('merchants.Merchant', verbose_name='商户id', on_delete=models.SET_NULL, null=True, blank=True)
    level = models.ForeignKey('users.Level', verbose_name='层级id', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_order_residential_hostel'
        verbose_name = '小美邮宿订单'
        verbose_name_plural = verbose_name


class APIWhiteTable(models.Model):
    '''ip白名单'''

    char = models.CharField(verbose_name='来源ID', max_length=32, null=True, blank=True)
    user = models.ForeignKey('users.User', verbose_name='用户', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = "tb_api_white_table"
        verbose_name = 'api白名单'
        verbose_name_plural = verbose_name


class ExpressType(models.Model):

    express_type = models.CharField(verbose_name='快递公司类型', max_length=32, null=True, blank=True)
    express_type_name = models.CharField(verbose_name='快递公司名称', max_length=32, null=True, blank=True)
    logo = models.CharField(verbose_name='logo', max_length=128, null=True, blank=True)

    class Meta:
        db_table = 'tb_order_express_type'
        verbose_name = '快递类型'
        verbose_name_plural = verbose_name


class Express(models.Model):
    '''快递单'''

    DELIVERY_STATUS_CHOICES = (
        (0, '在途'),
        (1, '揽收'),
        (2, '疑难'),
        (3, '已签收'),
        (4, '签退'),
        (5, '派件'),
        (6, '退回'),
        (7, '转投'),
        (8, '清关'),
        (9, '错误'),
        (14, '拒签')
    )

    NATURE_CHOICES = (
        (1, '寄货'),
        (2, '退货')
    )

    express_type = models.ForeignKey(ExpressType, on_delete=models.CASCADE, verbose_name="快递类型")
    express_number = models.CharField(verbose_name='快递单号', max_length=32)
    deliverystatus = models.SmallIntegerField(verbose_name='快递状态', choices=DELIVERY_STATUS_CHOICES, default=1, blank=True)
    create_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)
    sign_time = models.DateTimeField(verbose_name='签收时间', null=True, blank=True)
    nature = models.SmallIntegerField(verbose_name='快递状态', choices=NATURE_CHOICES, default=1)

    class Meta:
        db_table = 'tb_order_express'
        verbose_name = '快递单'
        verbose_name_plural = verbose_name


class ExpressOrderRelevance(models.Model):

    order = models.ForeignKey(OrderInfo, on_delete=models.CASCADE, verbose_name="订单")
    express = models.ForeignKey(Express, verbose_name='快递', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_order_express_relevance'
        verbose_name = '快递单'
        verbose_name_plural = verbose_name


class OrderAdvance(models.Model):
    '''推进历史'''

    ORDER_STATUS_CHOICES = (
        (1, "待审核"),
        (2, "待发货"),
        (3, "待收货"),
        (4, "已收货"),
        (5, "已完成"),
        (6, "已取消"),
        (7, "已驳回"),
        (8, "待结算")
    )

    order = models.ForeignKey('orders.OrderInfo', on_delete=models.CASCADE, verbose_name='订单')
    create_time = models.DateTimeField(verbose_name='推进时间', auto_now_add=True)
    status = models.SmallIntegerField(choices=ORDER_STATUS_CHOICES, default=1, verbose_name="订单状态")
    remark = models.TextField(verbose_name='说明', max_length=1280, null=True, blank=True)
    user = models.ForeignKey('users.User', on_delete=models.SET_NULL, verbose_name='操作人', null=True, blank=True)
    customeruser = models.ForeignKey('users.CustomerUser', on_delete=models.SET_NULL, verbose_name='操作人', null=True, blank=True)
    customeruservice = models.ForeignKey('users.CustomerUserVice', on_delete=models.SET_NULL, verbose_name='操作人', null=True, blank=True)
    merchantuser = models.ForeignKey('merchants.MerchantUser', on_delete=models.SET_NULL, verbose_name='操作人', null=True, blank=True)

    class Meta:
        db_table = "tb_order_advance"
        verbose_name = '推进历史'
        verbose_name_plural = verbose_name


class ExpressPlan(models.Model):
    '''快递进度'''

    express = models.ForeignKey(Express, verbose_name='快递', on_delete=models.CASCADE)
    create_time = models.DateTimeField(verbose_name='时间')
    remark = models.TextField(verbose_name='说明')

    class Meta:
        db_table = 'tb_order_express_plan'
        verbose_name = '快递进度'
        verbose_name_plural = verbose_name


class ExpressQuest(models.Model):
    '''导入快递任务表'''

    IS_ENTER_CHOICES = (
        (1, '处理中'),
        (2, '处理成功'),
        (3, '处理失败'),
        (4, '等待中'),
        (5, '已取消'),
        (6, '待审核')
    )

    user = models.ForeignKey('users.User', verbose_name='关联用户', on_delete=models.SET_NULL, null=True, blank=True)
    merchantuser = models.ForeignKey('merchants.MerchantUser', verbose_name='关联用户', on_delete=models.SET_NULL, null=True, blank=True)
    file = models.FileField(verbose_name='文件', null=True, blank=True)
    logs_file = models.FileField(verbose_name='日志文件', null=True, blank=True)
    create_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)
    is_enter = models.SmallIntegerField(verbose_name='状态', choices=IS_ENTER_CHOICES, default=1)
    line_number = models.IntegerField(verbose_name='行数', default=0)
    success_number = models.IntegerField(verbose_name='录入数', default=0)
    error_number = models.IntegerField(verbose_name='录入数', default=0)

    class Meta:
        db_table = 'tb_order_express_quest'
        verbose_name = '导入快递任务表'
        verbose_name_plural = verbose_name


class OrderSchengle(models.Model):
    '''盛大订单'''

    ORDER_STATE_CHOICES = (
        (1, '初始化'),
        (2, '下单成功'),
        (3, '下单失败'),
        (4, '部分核销'),
        (5, '已完结'),
        (6, '已退款')
    )

    order = models.OneToOneField('orders.OrderInfo', on_delete=models.CASCADE, verbose_name='订单')
    orderId = models.CharField(verbose_name='盛大订单id', max_length=32, null=True, blank=True)
    outTradeNo = models.CharField(verbose_name='盛大订单id', max_length=32)
    formCode = models.CharField(verbose_name='商品编码', max_length=32)
    create_time = models.DateTimeField(verbose_name='下单时间', null=True, blank=True)
    start_time = models.DateTimeField(verbose_name='有效时间(起)', null=True, blank=True)
    end_time = models.DateTimeField(verbose_name='有效时间(止)', null=True, blank=True)
    order_state = models.SmallIntegerField(verbose_name='订单状态', choices=ORDER_STATE_CHOICES, default=1)
    resultDesc = models.TextField(verbose_name='失败原因', null=True, blank=True)
    cancel_time = models.DateTimeField(verbose_name='取消时间', null=True, blank=True)
    cancel_user = models.ForeignKey('users.User', verbose_name='操作人', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_order_schengle'
        verbose_name = '盛大订单'
        verbose_name_plural = verbose_name


class OrderSchengleCoupons(models.Model):
    '''盛大订单'''

    COUPON_STATE_CHOICES = (
        (0, '初始化'),
        (1, '待核销'),
        (2, '预约中'),
        (3, '预约确认通过'),
        (4, '预约拒绝'),
        (5, '服务完成'),
        (6, '已过期'),
        (7, '用户发起取消')
    )

    order = models.ForeignKey('orders.OrderInfo', on_delete=models.CASCADE, verbose_name='订单')
    couponId = models.CharField(verbose_name='券码id', max_length=32, unique=True)
    serviceId = models.CharField(verbose_name='设备id', max_length=32)
    matrixContent = models.CharField(verbose_name='二维码加密串', max_length=64)
    createTime = models.DateTimeField(verbose_name='下单时间', null=True, blank=True)
    startTime = models.DateTimeField(verbose_name='有效时间(起)', null=True, blank=True)
    endTime = models.DateTimeField(verbose_name='有效时间(止)', null=True, blank=True)
    useTime = models.DateTimeField(verbose_name='使用时间', null=True, blank=True)
    coupon_state = models.SmallIntegerField(verbose_name='券状态', choices=COUPON_STATE_CHOICES, default=1)
    useShopCode = models.CharField(verbose_name='消费门店编码', max_length=32, null=True, blank=True)
    useShopName = models.CharField(verbose_name='消费门店名称', max_length=512, null=True, blank=True)
    useCityCode = models.CharField(verbose_name='消费城市编码', max_length=32, null=True, blank=True)
    useCityName = models.CharField(verbose_name='消费城市名称', max_length=32, null=True, blank=True)

    class Meta:
        db_table = 'tb_order_schengle_coupons'
        verbose_name = '盛大订单券码'
        verbose_name_plural = verbose_name


class OrderSchengleCouponsStore(models.Model):
    '''盛大订单可用门店'''

    STORE_STATUS_CHOICES = (
        (1, '正常'),
        (2, '变更中'),
        (3, '下线')
    )

    store_name = models.CharField(verbose_name='门店名称', max_length=128)
    store_code = models.CharField(verbose_name='门店编码', max_length=64)
    store_status = models.CharField(verbose_name='门店编码', max_length=16)
    provinceCode = models.CharField(verbose_name='所属省份编码', max_length=32)
    provinceName = models.CharField(verbose_name='所属省份编码', max_length=32)
    cityCode = models.CharField(verbose_name='所属省份名称', max_length=32)
    cityName = models.CharField(verbose_name='所属省份名称', max_length=32)
    areaCode = models.CharField(verbose_name='所属省份名称', max_length=32)
    areaName = models.CharField(verbose_name='所属省份名称', max_length=32)
    address = models.CharField(verbose_name='所属省份名称', max_length=256)
    bd_longitude = models.CharField(verbose_name='百度经度', max_length=32, null=True, blank=True)
    bd_latitude = models.CharField(verbose_name='百度纬度', max_length=32, null=True, blank=True)
    gd_longitude = models.CharField(verbose_name='高德经度', max_length=32, null=True, blank=True)
    gd_latitude = models.CharField(verbose_name='高德纬度', max_length=32, null=True, blank=True)
    contactPerson = models.CharField(verbose_name='联系人', max_length=64, null=True, blank=True)
    telephone = models.CharField(verbose_name='联系电话', max_length=32, null=True, blank=True)
    openTime = models.CharField(verbose_name='营业开始时间', max_length=16, null=True, blank=True)
    closingTime = models.CharField(verbose_name='营业结束时间', max_length=16, null=True, blank=True)
    uscc = models.CharField(verbose_name='社会信用代码', max_length=32, null=True, blank=True)
    grade = models.CharField(verbose_name='网点评级', max_length=16, null=True, blank=True)
    facadeImageUrl = models.CharField(verbose_name='门头图片', max_length=128, null=True, blank=True)
    logoImageUrl = models.CharField(verbose_name='logo', max_length=128, null=True, blank=True)
    loungeImageUrl = models.CharField(verbose_name='休息室图片', max_length=128, null=True, blank=True)
    workspaceImageUrl = models.CharField(verbose_name='工作区图片', max_length=128, null=True, blank=True)
    services = models.TextField(verbose_name='服务列表', null=True, blank=True)

    class Meta:
        db_table = 'tb_order_schengle_coupons_store'
        verbose_name = '盛大订单可用门店'
        verbose_name_plural = verbose_name